<template>
  <div class="article-container">
    <el-card class="filter-card">
      <div slot="header" class="clearfix">
        <!-- 面包屑路径导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item to="/">首页</el-breadcrumb-item>
          <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- /面包屑路径导航 -->
      </div>
      <!-- 数据筛选表单 -->
      <el-form ref="form" :model="form" label-width="40px" size="mini">
        <el-form-item label="状态">
          <el-radio-group v-model="status">
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
            <el-radio :label="4">已删除</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="频道">
          <el-select v-model="channelId" placeholder="请选择频道">
            <el-option
              :label="channel.name"
              :value="channel.id"
              v-for="(channel, index) in channels"
              :key="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="日期">
          <el-date-picker
            v-model="rangeDate"
            type="datetimerange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['12:00:00']"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
      <!-- /数据筛选表单 -->
    </el-card>

    <el-card class="box-card">
      <div slot="header" class="clearfix">
        根据筛选条件共查询到 {{ totalCount * 10 }} 条结果：
      </div>
      <!-- 数据列表 -->
      <!--
  Table 表格组件
  1、把需要展示的数组列表数据绑定给 table 组件的 data 属性
    注意：你不用去 v-for 遍历，它自己会遍历
  2、设计表格列 el-table-column
    width 可以设定表格列的宽度
    label 可以设定列的标题
    prop  用来设定要渲染的列表项数据字段，只能展示文本

  3、表格列默认只能渲染普通文本，如果需要展示其它内容，例如放个按钮啊、放个图片啊，那就需要自定义表格列模板了：https://element.eleme.cn/#/zh-CN/component/table#zi-ding-yi-lie-mo-ban
 -->
      <el-table
        v-loading="loading"
        :data="articles"
        stripe
        style="width: 100%"
        class="list-table"
        size="mini"
      >
        <el-table-column prop="date" label="封面">
          <template slot-scope="scope">
            <!-- <img 
      class="acticle-cover" 
      :src="scope.row.cover.images[0]" alt=""
       fit=" none"
       /> -->
            <!-- 利用v-if 和v-else影藏 -->
            <img
              v-if="scope.row.cover.images[0]"
              class="acticle-cover"
              :src="scope.row.cover.images[0]"
              alt=""
            />
            <img v-else class="acticle-cover" src="./no-img.png" alt="" />
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题"> </el-table-column>
        <el-table-column label="状态">
          <!-- 如果需要在自定义列模板中获取当前遍历项数据，那么就在 template 上声明 slot-scope="scope" -->
          <template slot-scope="scope">
            <el-tag :type="articleStatus[scope.row.status].type">
              {{ articleStatus[scope.row.status].text }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="pubdate" label="发布时间"> </el-table-column>
        <el-table-column label="操作">
          <!-- 如果需要自定义表格列模板，则把需要自定义的内容放到 template 里面 -->
          <template slot-scope="scope">
            <el-button
              size="mini"
              circle
              icon="el-icon-edit"
              type="primary"
              @click="$router.push('/publish?id=' + scope.row.id)"
            ></el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              circle
              @click="deleteArt(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- /数据列表 -->

      <!-- 列表分页 -->
      <el-pagination
        layout="prev, pager, next"
        background
        @current-change="onCurrentPage"
        :total="totalCount"
        :disabled="loading"
        :page-size="pageSize"
      >
      </el-pagination>
      <!-- /列表分页 -->
    </el-card>
  </div>
</template>

<script>
import { getArticles, getArticleChannels, deleteArticle } from "@/api/article";
export default {
  name: "ArticleIndex",
  components: {},
  props: {},
  data() {
    return {
      form: {
        name: "",
        region: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: "",
      },
      articles: [], //文章数据
      articleStatus: [
        { status: 0, text: "草稿", type: "info" }, // 0
        { status: 1, text: "待审核", type: "" }, // 1
        { status: 2, text: "审核通过", type: "success" }, // 2
        { status: 3, text: "审核失败", type: "warning" }, // 3
        { status: 4, text: "已删除", type: "danger" }, // 4
      ],
      totalCount: 0,
      pageSize: 20,
      status: null,
      channels: [], //频道数据
      channelId: null,
      rangeDate: [],
      loading: true,
    };
  },
  computed: {},
  watch: {},
  created() {
    this.loadArticles(1);
    this.loadArticleChannels();
  },
  mounted() {},
  methods: {
    loadArticles(page = 1) {
      getArticles({
        //下面都是传给后台的参数
        //获取后端的的分页数据
        page: page,
        per_page: this.pageSize,
        //状态
        status: this.status,
        channel_id: this.channelId,
        //来个非空判断
        begin_pubdate: this.rangeDate ? this.rangeDate[0] : null,
        end_pubdate: this.rangeDate ? this.rangeDate[1] : null,
      }).then((res) => {
        const { results, total_count: totalCount } = res.data.data;
        this.articles = results;
        this.totalCount = totalCount;
        //请求结束关闭loading
        this.loading = false;
      });
    },
    loadArticleChannels() {
      getArticleChannels().then((res) => {
        const { channels } = res.data.data;
        this.channels = channels;
      });
    },

    onSubmit() {
      this.loadArticles(1);
    },
    onCurrentPage(page) {
      this.loadArticles(page);
    },
    deleteArt(acticleId) {
      // console.log(acticleId.toString());
      //删除接口已被限制
      deleteArticle(acticleId.toString()).then(() => {
        this.$message({
          message: "删除成功",
          type: "warning",
        });
      });
    },
  },
};
</script>

<style scoped lang="less">
.filter-card {
  margin-bottom: 30px;
}

.list-table {
  margin-bottom: 20px;
}
.acticle-cover {
  width: 100px;
  height: 100px;
}
</style>